package com.gable.report.util;

import com.gable.report.model.ChartsAreaQuery;
import com.gable.report.model.User;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

import static com.gable.report.util.ChartsUtil.userRegionMap;

/**
 * Created by gable on 2018/5/10.
 */
@Component
public class RedisUtil<T> {

    //redis是否宕机开关
    public static boolean isOn = true;

    /**
     * @Author: 杨青桦
     * @Description: list类型存储
     * @Date: 2018/5/11 15:39
     * @param key
     * @return
     */
    public static List getListValue(RedisTemplate redisTemplate,String key){
        try {
            isOn = true;
            BoundListOperations listOperations = redisTemplate.boundListOps(key);
            if(listOperations.size() > 0){
                return listOperations.range(0,listOperations.size());
            }
        } catch (Exception e) {
            isOn = false;
            e.printStackTrace();
            return null;
        }
        return null;
    }

    /**
     * @Author: 杨青桦
     * @Description: value类型存储
     * @Date: 2018/5/30 11:05
     * @param redisTemplate
     * @param key
     * @param <T>
     * @return
     */
    public static<T> T getValue(RedisTemplate redisTemplate, String key){
        try {
            isOn = true;
            BoundValueOperations valueOperations = redisTemplate.boundValueOps(key);
            return (T)valueOperations.get();
        } catch (Exception e) {
            isOn = false;
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @Author: 杨青桦
     * @Description: 获取区报表筛选条件的key
     * @Date: 2018/5/28 13:36
     * @param user
     * @param tag
     * @return
     */
    public static String getAreaSearchKey(User user,String tag){
        return "area_" + userRegionMap.get(user.getId()) + "_search_" + tag;
    }

    /**
     * @Author: 杨青桦
     * @Description:  获取区报表各模块的Key
     * @Date: 2018/6/11 13:52
     * @param user
     * @param tag
     * @return
     */
    public static String getAreaModuleKey(String module,User user, String tag, ChartsAreaQuery chartsAreaQuery){
        String key = "";
        if(StringUtil.isEmpty(chartsAreaQuery.getTypeId()) && StringUtil.isEmpty(chartsAreaQuery.getYear())){
            key = "area_" + userRegionMap.get(user.getId()) + "_" + module + "_" + tag + "_year_" + LocalDate.now().getYear();
        }else if(!StringUtil.isEmpty(chartsAreaQuery.getYear()) &&  StringUtil.isEmpty(chartsAreaQuery.getTypeId()) ){
            key = "area_" + userRegionMap.get(user.getId()) + "_" + module + "_" + tag + "_year_" + chartsAreaQuery.getYear();
        }else if(StringUtil.isEmpty(chartsAreaQuery.getYear()) &&  !StringUtil.isEmpty(chartsAreaQuery.getTypeId())){
            key = "area_" + userRegionMap.get(user.getId()) + "_" + module + "_" + tag + "_year_" + LocalDate.now().getYear() + "_typeId_" + chartsAreaQuery.getTypeId();
        }else{
            key = "area_" + userRegionMap.get(user.getId()) + "_" + module + "_" + tag +  "_year_" + chartsAreaQuery.getYear()+"_typeId_" + chartsAreaQuery.getTypeId();
        }
        return key;
    }

}
